#include <stdio.h>
#include <stdlib.h>
#include <string.h>

#define MAX_TASKS 100
#define MAX_DESCRIPTION_LENGTH 100

typedef struct Task {
    int id;
    char description[MAX_DESCRIPTION_LENGTH];
    int isCompleted;
} Task;

Task taskList[MAX_TASKS];
int taskCount = 0;

void addTask(const char* description) {
    if (taskCount >= MAX_TASKS) {
        printf("Task list is full.\n");
        return;
    }
    taskList[taskCount].id = taskCount + 1;
    printf("Enter task description: ");
    taskList[taskCount].description[strcspn(taskList[taskCount].description, "\n")] = 0;
    taskList[taskCount].isCompleted = 0;
    taskCount++;
    printf("Task added successfully!\n");
}
void listTasks() {
    printf("\nList of Tasks:\n");
    for (int i = 0; i < taskCount; i++) {
        printf("%d. [%s] %s\n", taskList[i].id, taskList[i].isCompleted ? "X" : " ", taskList[i].description);
    }
}

void completeTask(int id) {
    for (int i = 0; i < taskCount; i++) {
        if (taskList[i].id == id) {
            taskList[i].isCompleted = 1;
            printf("Task %d marked as completed.\n", id);
            return;
        }
    }
    printf("Task with id %d not found.\n", id);
}

void deleteTask(int id) {
    for (int i = 0; i < taskCount; i++) {
        if (taskList[i].id == id) {
            for (int j = i; j < taskCount - 1; j++) {
                taskList[j] = taskList[j + 1];
            }
            taskCount--;
            printf("Task %d deleted successfully.\n", id);
            return;
        }
    }
    printf("Task with id %d not found.\n", id);
}


void loadTasks(const char* filename) {
    FILE* file = fopen(filename, "r");
    if (file == NULL) {
        // 如果文件不存在，就创建一个空的任务列表
        taskCount = 0;
        return;
    }
    int id, isCompleted;
    char description[MAX_DESCRIPTION_LENGTH];
    while (fscanf(file, "%d", &id) == 1) {
        if (taskCount >= MAX_TASKS) {
            printf("Task list is full. Excess tasks will not be loaded.\n");
            break;
        }
        taskList[taskCount].id = id;
        fscanf(file, "%s", description); // 读取描述
        taskList[taskCount].description[0] = '\0'; // 清空描述
        strcat(taskList[taskCount].description, description); // 添加描述
        fscanf(file, "%d", &isCompleted);
        taskList[taskCount].isCompleted = isCompleted;
        taskCount++;
    }
    fclose(file);
}

void saveTasks(const char* filename) {
    FILE* file = fopen(filename, "w");
    if (file == NULL) {
        perror("Error opening file");
        return;
    }
    for (int i = 0; i < taskCount; i++) {
        fprintf(file, "%d %s %d\n", taskList[i].id, taskList[i].description, taskList[i].isCompleted);
    }
    fclose(file);
}

int main(int argc, char* argv[]) {
    const char* filename = ".\\tasks.txt";
    
    // Load tasks from file on startup
    loadTasks(filename);
    
    if (argc < 2) {
        printf("Usage: %s <command> [arguments]\n", argv[0]);
        return 1;
    }

    if (strcmp(argv[1], "add") == 0 && argc == 3) {
        addTask(argv[2]);
    }
    else if (strcmp(argv[1], "list") == 0 && argc == 2) {
        listTasks();
    }
    else if (strcmp(argv[1], "complete") == 0 && argc == 3) {
        completeTask(atoi(argv[2]));
    }
    else if (strcmp(argv[1], "delete") == 0 && argc == 3) {
        deleteTask(atoi(argv[2]));
    }
    else {
        printf("Invalid command or arguments.\n");
        return 1;
    }

    // Save tasks to file on exit
    saveTasks(filename);
    return 0;
}